<html>
<head><meta charset="utf-8"><title>what&#x27;s an access? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what&#x27;s.20an.20access.3F.html">what&#x27;s an access?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="159268049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what%27s%20an%20access%3F/near/159268049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what&#x27;s.20an.20access.3F.html#159268049">(Feb 24 2019 at 08:59)</a>:</h4>
<p>Is:</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">y</span>: <span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="c1">// Is this an access?</span>
</pre></div>


<p>What about:</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">)</span>: <span class="p">(</span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">i32</span><span class="p">);</span><span class="w"></span>
<span class="n">ptr</span>::<span class="n">copy</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span><span class="w"> </span><span class="c1">// Is this an access?</span>
</pre></div>



<a name="159268118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what%27s%20an%20access%3F/near/159268118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what&#x27;s.20an.20access.3F.html#159268118">(Feb 24 2019 at 09:01)</a>:</h4>
<p>I thought that reading or writing some memory are accesses, but reading / writing no memory are not accesses. But that would make dereferencing a pointer to a ZST not an access.</p>
<p>This is why I don't understand the discussion about whether accesses to ZSTs are valid. I thought these weren't accesses at all. </p>
<p>If dereferencing a pointer to a ZST accesses something, what does it access? It does not access any memory or place.</p>



<a name="159272700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what%27s%20an%20access%3F/near/159272700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what&#x27;s.20an.20access.3F.html#159272700">(Feb 24 2019 at 11:30)</a>:</h4>
<blockquote>
<p><code>let y: () = *x; // Is this an access?</code></p>
</blockquote>
<p>this is a zero-sized access. the details for that are still somewhat unclear, see <a href="https://github.com/rust-rfcs/unsafe-code-guidelines/issues/93" target="_blank" title="https://github.com/rust-rfcs/unsafe-code-guidelines/issues/93">https://github.com/rust-rfcs/unsafe-code-guidelines/issues/93</a></p>



<a name="159272764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what%27s%20an%20access%3F/near/159272764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what&#x27;s.20an.20access.3F.html#159272764">(Feb 24 2019 at 11:33)</a>:</h4>
<div class="codehilite"><pre><span></span>let (x, y): (*mut i32, *mut i32);
ptr::copy(x, y, 0); // Is this an access?
</pre></div>


<p>no, this is not. but the pointer must still be aligned and non-NULL.</p>



<a name="159272847"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what%27s%20an%20access%3F/near/159272847" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what&#x27;s.20an.20access.3F.html#159272847">(Feb 24 2019 at 11:36)</a>:</h4>
<p>but yes, we could also rule that zero-sized "accesses" nor not accesses. and that's in fact what Miri implements. (Note that the ptr <em>does</em> need to be aligned though, the alignment check happens before actually doing an access.)</p>



<a name="159272888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what%27s%20an%20access%3F/near/159272888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what&#x27;s.20an.20access.3F.html#159272888">(Feb 24 2019 at 11:37)</a>:</h4>
<p>but that still leaves the problem with zero-offset GEP.  If zero-offset GEP is <em>always valid</em>, then I am fine also making zero-sized accesses "not really accesses". but imposing requirements on a zero-offset GEP and NOT on a zero-sized access, that is inconsistent IMO.</p>



<a name="159276003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what%27s%20an%20access%3F/near/159276003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what&#x27;s.20an.20access.3F.html#159276003">(Feb 24 2019 at 13:20)</a>:</h4>
<p>Hm, just to check I follow. Iff a dereference of a pointer to a ZST is an access, but <code>ptr::copy(x, y, 0)</code> is not, then that is because <code>ptr::copy</code> checks the length first (<code>0</code>), and dereferences no pointers, therefore it performs no accesses ?</p>



<a name="159279934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what%27s%20an%20access%3F/near/159279934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what&#x27;s.20an.20access.3F.html#159279934">(Feb 24 2019 at 15:28)</a>:</h4>
<p>hm, yeah that's how I was imagining it. but I do agree that it is also somewhat inconsistent.</p>



<a name="159279951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what%27s%20an%20access%3F/near/159279951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what&#x27;s.20an.20access.3F.html#159279951">(Feb 24 2019 at 15:28)</a>:</h4>
<p>the best solution to me seems to be to allow getelementptr-by-0 on all pointers, but I am not sure if LLVM is happy with that.</p>



<a name="159320981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what%27s%20an%20access%3F/near/159320981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/what&#x27;s.20an.20access.3F.html#159320981">(Feb 25 2019 at 08:32)</a>:</h4>
<p>I think I can follow the reasoning: pointer dereferences are the primitive operation here, not <code>copy</code>. <br>
I somehow expected it to be the other way around: pointer dereferences are not the primitive operation, memory copying is. A pointer dereference just copies memory from some address somewhere else.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>